package base.search.leetcode;

import base.sort.BubbleSort;
import base.sort.InsertSort;
import base.sort.SelectionSort;
import base.utils.PrintUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;

/**
 * 参考  LeetCode - 1065字符串的索引对
 * https://blog.csdn.net/qq_29051413/article/details/108709218
 *
 *
 */

public class StringIndexDemo {
    static int[]arrays={6 , 2, 3, 1, 8 , 5, 4 , 0};

    public static void main(String[] args) {
        selectionSortDemo01();

    }






    public static void findTextWords(){
        String text = "thestoryofleetcodeandme";

        String[] wordsList = new String[] { "story" , "fleet" ,};
        String[] words = { "story" , "fleet" , "leetcode"};

        int[] nums ={1,2,3,4,5};

        int[][] pairsList = indexPairs(text , words);
        findWord(text , "story");
        findWord(text , "fleet");
        findWord(text , "leetcode");

        System.out.println(pairsList);
    }


//    [3,7]   [9 , 13]   [10 , 17]



    public static boolean cmp(int[] a , int[] b){
        if (a[0] == b[0])
            return a[1] < b[1];
        return a[0] < b[0];
    }

    public static int[][] indexPairs(String text , String[] words){
        int wordSize;
        List<Integer[]>lists = new ArrayList<>();

        for (String word : words){
            wordSize = word.length();
            for (int i = 0; i <= text.length() - wordSize ; i++){
                if (word.equals(text.substring(i , i + wordSize))){
                    Integer[] target = new Integer[2];
                    target[0] = i;
                    target[1] = i + wordSize -1;
                    lists.add(target);
                }
            }
        }
        int [][] ans = new int[lists.size()][2];

        for (int j = 0 ; j < lists.size() ; j++){
            ans[j][0] = lists.get(j)[0];
            ans[j][1] = lists.get(j)[1];
        }

        Arrays.sort(ans, new Comparator<int[]>() {
            @Override
            public int compare(int[] o1, int[] o2) {
                if (o1[0] != o2[0]){
                    return o1[0] - o2[0];
                }else {
                    return o1[1] - o2[1];
                }
            }
        });
        return ans;
    }


    /**
     *  从 text 中找到 word的坐标(pair)，
     * @param text
     * @param word
     */
    public static void findWord(String text , String word){
        int textSize = text.length();
        int wordSize = word.length();

        int endSize = textSize - wordSize;
        for (int i = 0 ; i < endSize; i ++){
            // 对text进行截取
            String subStr = text.substring(i , i + wordSize);

            System.out.println("subStr = " + subStr);

            if (word.equals(subStr)) {
                System.out.println("find " + word + "; start = " + i + ";end = " + (i + wordSize -1));
                break;
            }
        }
        System.out.println("---------------------------------");
    }



    public static void checkBubleSort(){
        int[] newArray = BubbleSort.bubbleSortDemo1(arrays);
        PrintUtils.print(newArray);
    }


  public static void selectionSortDemo01(){
        int[] newArray = SelectionSort.selectionSortDemo01(arrays);
        PrintUtils.print(newArray);
    }






}
